<template>
  <div class="order-result">
    <el-card class="box-card">
      <div class="result-content">
        <div class="result-icon">
          <el-icon :size="64" :color="status === 'success' ? '#67c23a' : '#f56c6c'">
            <component :is="status === 'success' ? 'CircleCheckFilled' : 'CircleCloseFilled'" />
          </el-icon>
        </div>
        <div class="result-title">
          {{ status === 'success' ? '支付成功' : '支付失败' }}
        </div>
        <div class="result-info" v-if="status === 'success'">
          <p>订单号：{{ orderSn }}</p>
          <p>支付金额：¥{{ payAmount }}</p>
          <p>支付方式：{{ getPayTypeText(payType) }}</p>
          <p>支付时间：{{ payTime }}</p>
        </div>
        <div class="result-actions">
          <el-button type="primary" @click="viewOrder">
            查看订单
          </el-button>
          <el-button @click="goHome">
            返回首页
          </el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getOrder } from '@/api/order'
import { CircleCheckFilled, CircleCloseFilled } from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()

const status = ref(route.query.status || 'success')
const orderSn = ref('')
const payAmount = ref(0)
const payType = ref(1)
const payTime = ref('')

// 获取订单信息
const getOrderInfo = async () => {
  try {
    const sn = route.query.orderSn
    if (!sn) {
      ElMessage.error('订单编号不存在')
      router.push('/order/list')
      return
    }
    const res = await getOrder(sn)
    if (res.code === 200) {
      const orderData = res.data
      orderSn.value = orderData.orderSn
      payAmount.value = orderData.payAmount
      payType.value = orderData.payType
      payTime.value = orderData.payTime
    } else {
      ElMessage.error(res.msg || '获取订单信息失败')
    }
  } catch (error) {
    console.error('获取订单信息失败:', error)
    ElMessage.error('获取订单信息失败')
  }
}

// 获取支付方式文本
const getPayTypeText = (type) => {
  const payTypeMap = {
    1: '支付宝支付',
    2: '微信支付'
  }
  return payTypeMap[type] || '未知支付方式'
}

// 查看订单
const viewOrder = () => {
  router.push('/order/list')
}

// 返回首页
const goHome = () => {
  router.push('/')
}

onMounted(() => {
  if (route.query.orderSn) {
    getOrderInfo()
  } else {
    router.push('/order/list')
  }
})
</script>

<style lang="scss" scoped>
.order-result {
  padding: 20px;
  background-color: #f5f5f5;
  min-height: calc(100vh - 60px);
  display: flex;
  align-items: center;
  justify-content: center;

  .box-card {
    width: 100%;
    max-width: 600px;
  }

  .result-content {
    text-align: center;
    padding: 40px 20px;

    .result-icon {
      margin-bottom: 20px;
    }

    .result-title {
      font-size: 24px;
      font-weight: bold;
      margin-bottom: 20px;
      color: #333;
    }

    .result-info {
      margin-bottom: 30px;
      
      p {
        margin: 10px 0;
        color: #666;
        font-size: 14px;
      }
    }

    .result-actions {
      .el-button {
        margin: 0 10px;
      }
    }
  }
}
</style>